<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="主表ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainId">
              <a-input :disabled="formDisabled" v-model="model.mainId" :max-length="36" placeholder="请输入主表ID" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="款项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentName">
              <a-input :disabled="formDisabled" v-model="model.paymentName" :max-length="36" placeholder="请输入款项名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="应收款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receivableAmount">
              <a-input-number
                :disabled="formDisabled"
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="model.receivableAmount"
                placeholder="请输入应收款金额"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="已开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoicingAmount">
              <a-input-number
                :disabled="formDisabled"
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="model.invoicingAmount"
                placeholder="请输入已开票金额"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="已回款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refundedAmount">
              <a-input-number
                :disabled="formDisabled"
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="model.refundedAmount"
                placeholder="请输入已回款金额"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ErpAuditTransferApplicationInfoForm',
    components: {
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model: {},
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: '/api/erp/audit/auditTransferApplication/erpAuditTransferApplicationInfo/add',
          edit: '/api/erp/audit/auditTransferApplication/erpAuditTransferApplicationInfo/edit',
          queryById: '/api/erp/audit/auditTransferApplication/erpAuditTransferApplicationInfo/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
                that.$emit('unButtonDisable');
              }
            }).catch(() => {
              that.$emit('unButtonDisable');
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$emit('unButtonDisable');
          }
        })
      }
    }
  }
</script>
